package com.testdocs.client.structure.general;

import com.google.gwt.activity.shared.ActivityManager;
import com.testdocs.client.ClientFactory;
import com.testdocs.client.project.event.ProjectDeselectedEvent;
import com.testdocs.client.project.event.ProjectDeselectedEventHandler;
import com.testdocs.client.project.event.ProjectSelectedEvent;
import com.testdocs.client.project.event.ProjectSelectedEventHandler;

public class GeneralPanelsPresenter implements GeneralPanels.Presenter {
	private ActivityManager activityManager;
	private ClientFactory clientFactory;
	
	public GeneralPanelsPresenter(ClientFactory clientFactory, ActivityManager activityManager) {
		this.clientFactory = clientFactory;
		this.activityManager = activityManager;
		
		clientFactory.getEventBus().addHandler(ProjectSelectedEvent.TYPE, new ProjectSelectedHandler());
		clientFactory.getEventBus().addHandler(ProjectDeselectedEvent.TYPE, new ProjectDeselectedHandler());
		
		clientFactory.getGeneralPanels().setPresenter(this);
		switchToProjectNotSelected();
	}
	
	public void switchToProjectSelected() {
		clientFactory.getGeneralPanels().setWorkArea(clientFactory.getWorkAreaPanel());
		activityManager.setDisplay(clientFactory.getWorkAreaPanel().getMain());
	}
	
	public void switchToProjectNotSelected() {
		activityManager.setDisplay(clientFactory.getGeneralPanels().getWorkAreaForDisplay());				
	}
	
	public class ProjectSelectedHandler implements ProjectSelectedEventHandler {
		@Override
		public void projectSelected(ProjectSelectedEvent event) {
			switchToProjectSelected();
		}
	}
	
	public class ProjectDeselectedHandler implements ProjectDeselectedEventHandler {
		@Override
		public void projectDeselected(ProjectDeselectedEvent event) {
			switchToProjectNotSelected();
		}
	}
}
